import fetch from '@blueos.network.fetch'
import prompt from '@blueos.window.prompt'
import { ipAddress } from './config'

export const requestLLM = (/** @type {number} */ strategy,otherParam)=>{
    if (strategy==0){
        return requestSpeechRecognizeLLM()
    } else if(strategy==1){
        return '语音合成url'
    } else if(strategy==2){
        requestTextLLM(otherParam).then((res)=>{
            return res
        })
    }
    return "访问 LLM 策略错误"
}

export const requestSpeechRecognizeLLM = (param)=>{
    return new Promise((resolve,reject)=>{
        fetch.fetch({
            url: 'http://'+ipAddress+':8080/llm/speech/recognize',
            method:'POST',
            data:{
                'filePath':param
            },
            responseType: 'text',
            success: function (response) {
                console.log(`${String(response.data)}`)
                resolve(Number(response.data))
            },
            fail: function (data, code) {
                prompt.showToast({
                    message:`errMsg = ${data},errCode = ${code}`
                })
                console.log(`handling fail, errMsg = ${data}`)
                console.log(`handling fail, errCode = ${code}`)
                resolve(`errMsg = ${data},errCode = ${code}`)
            },
        })
    })
    
}

export const requestTextLLM = (text)=>{
    return new Promise((resolve,reject)=>{
        fetch.fetch({
            url: 'http://'+ipAddress+':8080/llm/text',
            method:'POST',
            data:{
                'prompt':text
            },
            responseType: 'text',
            success: function (response) {
                resolve(String(response.data))
            },
            fail: function (data, code) {
                // console.log(`handling fail, errMsg = ${data}`)
                // console.log(`handling fail, errCode = ${code}`)
                resolve("fail")
            }
        })
        // resolve("未调用文本大模型")
    })
    
}

export const requestVisionModel = (param)=>{
    return new Promise((resolve,reject)=>{
        fetch.fetch({
            url: 'http://'+ipAddress+':8080/llm/vision',
            method:'POST',
            data:{
                'panelData':param
            },
            responseType: 'text',
            success: function (response) {
                // console.log(`${Number(response.data)}`)
                resolve(Number(response.data))
            },
            fail: function (data, code) {
                console.log(`handling fail, errMsg = ${data}`)
                console.log(`handling fail, errCode = ${code}`)
                // 随机返回一个 0~10之间的整数
                resolve( Math.round(Math.random()*10))
            },
        })
    })
    
}